using DimensionsHelper.LanguageService.Symbols;
using DimensionsHelper.LanguageService.Syntax;

namespace DimensionsHelper.LanguageService;

public class AnalysisSelectCaseClause : AnalysisStatement
{
    private readonly List<AnalysisExpression> _comparisons = [];

    internal AnalysisSelectCaseClause(SelectCaseClause syntaxNode,
        IReadOnlyList<AnalysisExpression> comparisons,
        AnalysisBlock body,
        AnalysisNode? parent) :
        base(AnalysisBoundKinds.SelectCaseClause, syntaxNode, parent)
    {
        Syntax = syntaxNode;
        Body = body;
        Body.Parent = this;

        foreach (AnalysisExpression? expr in comparisons)
        {
            _comparisons.Add(expr);
            expr.Parent = this;
        }
    }

    public AnalysisBlock Body { get; }

    public override int ChildrenCount => _comparisons.Count + 1;

    public override SelectCaseClause Syntax { get; }

    public override IEnumerable<AnalysisNode> GetChildren()
    {
        return [.._comparisons, Body];
    }

    internal override void AddChild(AnalysisNode child)
    {
        throw new NotSupportedException("AnalysisSelectCaseClause is read-only.");
    }
}

public static partial class AnalysisNodeFactory
{
    /// <summary>
    /// 创建一个Select Case分句语句
    /// </summary>
    /// <param name="syntaxNode">原始语法节点</param>
    /// <param name="comparisons">比较表达式集合</param>
    /// <param name="body">语句体</param>
    /// <param name="parent">父节点，可以忽略</param>
    /// <param name="withSymbol">语法节点所在的With语句上下文类型符号</param>
    public static AnalysisSelectCaseClause SelectCaseClause(SelectCaseClause syntaxNode,
        IReadOnlyList<AnalysisExpression> comparisons,
        AnalysisBlock body,
        AnalysisNode? parent,
        ITypeSymbol? withSymbol)
    {
        return new AnalysisSelectCaseClause(syntaxNode, comparisons, body, parent).WithContainingWithSymbol(withSymbol);
    }
}